package com.geekstudy.week7;

import java.sql.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Week7Work9 {

    private Map<String, Connection> connectionMap = new LinkedHashMap<String, Connection>();

    public void addConnection(String connectionId, String url, String username, String password) throws SQLException {

        Connection connection = DriverManager.getConnection(url, username, password);
        connectionMap.put(connectionId, connection);
    }




    public static void main(String[] args) {
        Week7Work9 work9 = new Week7Work9();

        try {
            String url1 = "jdbc:mysql://127.0.0.1:3306/geek_java4";
            String username1 = "java4";
            String password1 = "java4";
            work9.addConnection(username1, url1, username1, password1);

            String url2 = "jdbc:mysql://127.0.0.1:3306/geek_java4_demo1";
            String username2 = "java4demo1";
            String password2 = "java4demo1";
            work9.addConnection(username2, url2, username2, password2);

            int testSize = 6;
            List<String> connectionNameList = new ArrayList<String>();
            connectionNameList.addAll(work9.connectionMap.keySet());

            for(int size = 0; size <= testSize; size++) {
                int index = size % 2;
                String connectionName = connectionNameList.get(index);
                Connection connection = work9.connectionMap.get(connectionName);
                PreparedStatement preparedStatement = connection.prepareStatement("select count(*) as num from shop_order");
                ResultSet resultSet = preparedStatement.executeQuery();

                int queryNumber = -1;
                if(resultSet.next()) {
                    queryNumber = resultSet.getInt("num");
                }
                System.out.println(" size: " + size + " connectionName:" + connectionName + " queryNumber:" + queryNumber);
            }


        } catch(Exception e) {
            e.printStackTrace();
        }


    }
}
